<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent
            v-show="showSearch" label-width="68px">
            <el-form-item label="托盘号" prop="trayNo">
                <el-input v-model="queryParams.trayNo" placeholder="请输入托盘号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="批次号" prop="batchNo">
                <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"-->
<!--                    v-hasPermi="['bill:tray:add']">新增</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"-->
<!--                    v-hasPermi="['bill:tray:edit']">修改</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"-->
<!--                    @click="handleDelete" v-hasPermi="['bill:tray:remove']">删除</el-button>-->
<!--            </el-col>-->
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['bill:tray:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table ref="table" :height="calcTableHeight" v-loading="loading" :data="trayList"
            @selection-change="handleSelectionChange" highlight-current-row @current-change="rowSelection">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" width="50" />
            <el-table-column label="托盘号" align="center" prop="trayNo" min-width="120" show-overflow-tooltip />
            <el-table-column label="库存状态" align="center" prop="stockStatus" width="80" show-overflow-tooltip >
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.erp_stock_status" :value="scope.row.stockStatus" />
                </template>
            </el-table-column>
            <el-table-column label="批次号" align="center" prop="batchNo" width="120" show-overflow-tooltip />
            <el-table-column label="入库单据号" align="center" prop="inBillNo" width="150" show-overflow-tooltip />
            <el-table-column label="出库单据号" align="center" prop="outBillNo" width="150" show-overflow-tooltip />
            <el-table-column label="分拣单据号" align="center" prop="splitBillNo" width="150" show-overflow-tooltip />
            <el-table-column label="ERP入库单据号" align="center" prop="erpInBillNo" width="150" show-overflow-tooltip />
            <el-table-column label="ERP出库单据号" align="center" prop="erpOutBillNo" width="150" show-overflow-tooltip />
            <el-table-column label="备注" align="center" prop="remark" min-width="120" show-overflow-tooltip />
            <el-table-column label="创建人" align="center" prop="createBy" width="80" show-overflow-tooltip />
            <el-table-column label="创建时间" align="center" prop="createTime" width="155" show-overflow-tooltip />
            <el-table-column label="最后更新人" align="center" prop="updateBy" width="100" show-overflow-tooltip />
            <el-table-column label="最后更新时间" align="center" prop="updateTime" width="155" show-overflow-tooltip />
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改托盘对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="托盘号" prop="trayNo">
                    <el-input v-model="form.trayNo" placeholder="请输入托盘号" />
                </el-form-item>
                <el-form-item label="批次号" prop="batchNo">
                    <el-input v-model="form.batchNo" placeholder="请输入批次号" />
                </el-form-item>
                <el-form-item label="入库单据号" prop="inBillNo">
                    <el-input v-model="form.inBillNo" placeholder="请输入入库单据号" />
                </el-form-item>
                <el-form-item label="出库单据号" prop="outBillNo">
                    <el-input v-model="form.outBillNo" placeholder="请输入出库单据号" />
                </el-form-item>
                <el-form-item label="分拣单据号" prop="splitBillNo">
                    <el-input v-model="form.splitBillNo" placeholder="请输入分拣单据号" />
                </el-form-item>
                <el-form-item label="ERP入库单据号" prop="erpInBillNo">
                    <el-input v-model="form.erpInBillNo" placeholder="请输入ERP入库单据号" />
                </el-form-item>
                <el-form-item label="ERP出库单据号" prop="erpOutBillNo">
                    <el-input v-model="form.erpOutBillNo" placeholder="请输入ERP出库单据号" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!--   单据明细 -->
        <el-tabs v-model="activeTab" type="card" ref="halfTable" class="half-table">
            <el-tab-pane label="托盘明细" name="detailList">
                <detailPage ref="detailPage" style="padding: unset" :orderData="orderData" />
            </el-tab-pane>
        </el-tabs>


    </div>
</template>

<script>
import { listTray, getTray, delTray, addTray, updateTray } from "@/api/basic/billTray";
import detailPage from "./detail"
import { mixins } from "@/mixin/pageSearchTable";
export default {
    name: "Tray",
    components: { detailPage },
    mixins: [mixins],
    dicts: ["erp_stock_status"],
    data() {
        return {
            activeTab: "detailList",
            orderData: {},
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            selections: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 托盘表格数据
            trayList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                trayNo: null,
                stockStatus: null,
                batchNo: null,
                inBillNo: null,
                outBillNo: null,
                splitBillNo: null,
                erpInBillNo: null,
                erpOutBillNo: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                trayNo: [
                    { required: true, message: "托盘号不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        //行选择
        rowSelection(row) {
            this.orderData = row
        },


        /** 查询托盘列表 */
        getList() {
            this.loading = true;
            listTray(this.queryParams).then(response => {
                let orderData = { ...this.orderData }

                this.trayList = response.rows;
                this.total = response.total;
                this.loading = false;
                this.$nextTick(() => {
                    if (orderData && orderData.id) {
                        let row = response.rows.find(item => item.id == orderData.id)
                        this.$refs.table.setCurrentRow(row);
                    }
                })


            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                trayNo: null,
                stockStatus: "0",
                batchNo: null,
                inBillNo: null,
                outBillNo: null,
                splitBillNo: null,
                erpInBillNo: null,
                erpOutBillNo: null,
                remark: null,
                createBy: null,
                updateBy: null,
                updateTime: null,
                createTime: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.selections = selection
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加托盘";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getTray(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改托盘";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateTray(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addTray(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.resetQuery();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除"' + ids.length + '"条数据项？').then(function () {
                return delTray(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('bill/tray/export', {
                ...this.queryParams
            }, `tray_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
